//
//  CircleRotationModifier.swift
//  TimMusic
//
//  Created by nkl on 2024/10/22.
//

import Foundation
import WidgetKit
import SwiftUI
import CustomAPI

extension View {
    // app内使用rotate动画旋转， 桌面组件使用私有api
    func makeCircleRotation(inApp: Bool = false,
                             isActive: Bool,
                             mode: RotationMode = .second,
                             time: TimeZone = .current,
                             anchor: UnitPoint = .center) -> some View {
        if isActive {
            return AnyView(
                self.modifier(CircleRotationModifier(mode: mode, time: time, anchor: anchor))
                    .disableFlipAnimate(true)
            )
        }
        return AnyView(self)
    }
    
    func disableFlipAnimate(_ disable: Bool) -> some View {
        if #available(iOS 16.0, *) {
            if disable {
                return AnyView( self.contentTransition(.identity) )
            }
        }
        return AnyView( self )
    }
}

struct CircleRotationModifier: ViewModifier {
    var mode: RotationMode = .second
    var time: TimeZone = .current
    var anchor: UnitPoint = .center
    
//    @Environment(\.entry) var entryStore
    
    func body(content: Content) -> some View {
//        if entryStore.entry != nil || entryStore.isHomeScreen {
            content
                .animatedRotating(mode, in: time, anchor: anchor)
//        } else {
//            content
//                .modifier(AppCircleRotationModifier(duration: mode.animateDuration, rotateAngle: mode.beginAngle, anchor: anchor, clockwise: mode.isClockwise))
//        }
    }
}

struct AppCircleRotationModifier: ViewModifier {
    var duration: TimeInterval = 0
    var rotateAngle: Double = 0
    var anchor: UnitPoint = .center
    var clockwise: Bool = true
    @State var flag: Bool = true
    
    @State private var didAddAnimated: Bool = false
    
    func body(content: Content) -> some View {
        content
            .rotationEffect(Angle(degrees: rotateAngle), anchor: anchor)
            .rotationEffect(Angle(degrees: self.flag ? 0 : (self
                .clockwise ? 360 : -360)), anchor: anchor)
            .onAppear {
                if !didAddAnimated {
                    self.didAddAnimated = true
                    withAnimation(Animation.linear(duration: duration).repeatForever(autoreverses: false)) {
                        self.flag.toggle()
                    }
                }
            }
    }
}

extension RotationMode {
    var beginAngle: Double {
        let cps = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())
        switch self {
        case .hour:
            let hourAngle = Double((cps.hour ?? 0) % 12) / 12.0 * 360.0
            let minuteAngle = Double((cps.minute ?? 0)) / 60.0 * (360.0 / 12.0)
            return hourAngle + minuteAngle
        case .minute:
            return Double(cps.minute ?? 0) / 60.0 * 360.0
        case .second:
            return Double(cps.second ?? 0) / 60.0 * 360.0
        case .custom(_):
            return 0
        @unknown default:
            return 0
        }
    }
    
    var animateDuration: TimeInterval {
        switch self {
        case .hour:
            return 12 * 60 * 60
        case .minute:
            return 60 * 60
        case .second:
            return 60
        case let .custom(time):
            return abs(time)
        default:
            return 0
        }
    }
    
    var isClockwise: Bool {
        if case let .custom(time) = self {
            return time > 0
        }
        return true
    }
}
